//
//  WJComposeViewController.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/28.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit

class WJComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
      
        setupUI()
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        view.backgroundColor = UIColor.whiteColor()
        setupNav()
        // 01 添加控件
        view.addSubview(composeTextView)
        composeTextView.addSubview(composePictureView)
        view.addSubview(composeToolbarView)
        // 02 添加约束
        // 01
        composeTextView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        // 02
        composePictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: SCREENW - 20, height: SCREENW - 20))
            make.top.equalTo(composeTextView).offset(100)
            make.centerX.equalTo(composeTextView)
        }
        // 03
        composeToolbarView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(44)
            make.leading.trailing.bottom.equalTo(view)
        }
        
        // 监听按钮点击
        composeToolbarView.closure = {(type: WJComposeToolbarViewType)->() in
            
            switch type{
            case .Picture:
                print("图片")
                self.selectImage()
            case .Mention:
                print("@")
            case .Trend:
                print("#")
            case .Emoticon:
                print("笑脸")
            case .Add:
                print("+")
                
            }
            
        }
        composePictureView.closure = {[weak self] in
            
            self?.selectImage()
        }
    }
    // MARK: - 设置导航
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", imgName: nil, target: self, action: "cancelAciton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", imgName: nil, target: self, action: "sendAciton")
        //
        navigationItem.rightBarButtonItem?.enabled = true
        navigationItem.titleView = titleView
    }
    // 取消
    func cancelAciton(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    // 发布
    func sendAciton(){

    }
    
    // MARK: - 懒加载控件
    private lazy var titleView: UILabel = {
        // 用户名字
        let name = WJUserAccountViewModel.shareTools.userAccoun?.screen_name ?? ""
        let str = "发微博\n\(name)"
        // 获取range
        let range = (str as NSString).rangeOfString(name)
        // 创建一个可变的富文本
        let attr = NSMutableAttributedString(string: str)
        // 设置富文本
        attr.addAttributes([NSForegroundColorAttributeName: ThemeColor,NSFontAttributeName: UIFont.systemFontOfSize(12)], range: range)
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.attributedText = attr
        lab.sizeToFit()
        return lab
    }()
    // 自定义textView
    //  在ios7 以后 如果系统检测到有导航栏 直接帮textView 的输入文字向下移动
    private lazy var composeTextView: WJComposeTextView = {
        let view = WJComposeTextView()
        view.delegate = self
        view.font = UIFont.systemFontOfSize(15)
        view.placeholder = "上海三期微博上海三期微博上海三期微博上海三期微博上海三期微博上海三期微博上海三期微博"
        view.alwaysBounceVertical = true
        return view
    }()
    // 配图
    private lazy var composePictureView: WJComposePictureView = WJComposePictureView()
    // 底部视图
    private lazy var composeToolbarView: WJComposeToolbarView = WJComposeToolbarView()
}

extension WJComposeViewController {
    
    func selectImage() {
        
        let pickerVC = UIImagePickerController()
        pickerVC.delegate = self
        self.presentViewController(pickerVC, animated: true, completion: nil)
        
    }
    
}

extension WJComposeViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        
        
        composePictureView.addImages(image.getImageScale(600))
 
        picker.dismissViewControllerAnimated(true, completion: nil)
        
    }
}

// MARK: - UITextViewDelegate
extension WJComposeViewController: UITextViewDelegate{
    func textViewDidChange(textView: UITextView) {
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 取消第一响应
        self.composeTextView.resignFirstResponder()
    }
}